package cst.a703xmq.zyfypt_703xmq.Activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;


import cst.a703xmq.zyfypt_703xmq.R;
import cst.a703xmq.zyfypt_703xmq.Model.ResModel;
import cst.a703xmq.zyfypt_703xmq.Interface.XmqListener;

public class ResActivity extends AppCompatActivity {
    private EditText u_name;
    private EditText p_word;
    private EditText realname;
    private RadioButton rb_stu;
    private EditText email;
    private EditText tel;
    private Button btn_back_login;
    private Button btn_sure_reg;
    private ResModel model;
    private XmqListener<String> listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_res);
        init();

        listener = new XmqListener<String>() {
            @Override
            public void onResponse(String list) {
                if(list.equals("1"))
                {
                    Toast.makeText(ResActivity.this, "注册成功...",
                            Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(ResActivity.this,"用户名已存在",
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ResActivity.this,msg,
                        Toast.LENGTH_SHORT).show();
            }
        };

        btn_sure_reg.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                model = new ResModel("scalar");
                if(rb_stu.isChecked())
                    model.reg(u_name.getText().toString(),
                            p_word.getText().toString(),
                            tel.getText().toString(),
                            2,
                            realname.getText().toString(),
                            email.getText().toString(),listener);
                else
                    model.reg(u_name.getText().toString(),
                            p_word.getText().toString(),
                            tel.getText().toString(),
                            3,
                            realname.getText().toString(),
                            email.getText().toString(),listener);
            }
        });

        btn_back_login.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                finish();
            }
        });
    }
    private void init()
    {
        u_name = (EditText)findViewById(R.id.reg_username);
        p_word = (EditText)findViewById(R.id.reg_password);
        realname = (EditText)findViewById(R.id.reg_realname);
        rb_stu = (RadioButton)findViewById(R.id.res_rBtn_stu);

        email = (EditText)findViewById(R.id.reg_email);
        tel = (EditText)findViewById(R.id.reg_phone);
        btn_back_login = (Button)findViewById(R.id.btn_back_login);
        btn_sure_reg = (Button)findViewById(R.id.btn_sure_reg);
    }
}
